home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mint99s / memprot.c < prev    next >
C/C++ Source or Header  |  1992-12-23  |  32KB  |  1,145 lines

  1. /*
  2.  * Copyright 1991,1992 Atari Corporation.
  3.  * All rights reserved.
  4.  */
  5.  
  6. /*
  7.  * page-table data structures
  8.  *
  9.  *
  10.  * The root pointer points to a list of pointers to top-level pointer tables.
  11.  * 
  12.  * Each entry in a pointer table points to another pointer table or to
  13.  * a page table, or is a page descriptor.
  14.  * 
  15.  * Since, initially, the logical address space is the physical address space,
  16.  * we only need to worry about 26MB plus 32K for I/O space.
  17.  * 
  18.  * Since we want some pages to be supervisor-accessible, but we don't want
  19.  * a whole separate table for that, we use long-format descriptors.
  20.  * 
  21.  * Initial memory map:
  22.  * 
  23.  * 0     - membot: S (supervisor only)
  24.  * membot     - memtop: P (protected TPA)
  25.  * memtop     - phystop: G (screen)
  26.  * phystop     - $00E00000: bus error
  27.  * $00E00000- $00E3FFFF: G (ROM)
  28.  * $00E40000- $00FF7FFF: bus error
  29.  * $00FF8000- $00FFFFFF: G (mostly S: I/O space, but that's done in hardware)
  30.  * $01000000- ramtop: P
  31.  * ramtop     - $7FFFFFFF: G (A32/D32 VME, cacheable)
  32.  * $80000000- $FEFFFFFF: G (A32/D32 VME, non cacheable)
  33.  * $FFxxxxxx          just like $00xxxxxx.
  34.  * 
  35.  * Here's a final choice of layouts: IS=0, PS=13 (8K), TIA=4, TIB=4, TIC=4,
  36.  * TID=7.  This lets us map out entire unused megabytes at level C, and gives
  37.  * us an 8K page size, which is the largest the '040 can deal with.
  38.  * 
  39.  * This code implements 4+4+4+7, as follows:
  40.  * 
  41.  * tbl_a
  42.  *     0 -> tbl_b0
  43.  *     1-7 -> Cacheable direct (VME) page descriptors
  44.  *     8-E -> Non-cacheable direct (VME) page descriptors
  45.  *     F -> tbl_bf
  46.  * 
  47.  * tbl_b0 table: 16 entries (assumes only 16MB of TT RAM)
  48.  *     0 -> tbl_c00 (16MB of ST RAM address space)
  49.  *     1 -> tbl_c01 (16MB of TT RAM address space)
  50.  *     2-F -> cacheable direct (VME) page descriptors
  51.  * 
  52.  * tbl_bF table: 16 entries (deals with $FF mapping to $00)
  53.  *     0-E -> Non-cacheable direct (VME) page descriptors
  54.  *     F -> tbl_c00 (16MB of ST RAM address space, repeated here as $FF)
  55.  * 
  56.  * tbl_c00 table: ST RAM address space (example assuming 4MB ST RAM)
  57.  *     0-3 -> RAM page tables
  58.  *     4-D -> invalid
  59.  *     E -> direct map, cache enable (ROM)
  60.  *     F -> direct map, cache inhibit (I/O)
  61.  * 
  62.  * For each 16MB containing any TT RAM, there's a tbl_c.  Within those,
  63.  * for each MB that actually has TT RAM, there's another table, containing
  64.  * 128 RAM page tables.  Where there isn't RAM, there are "global"
  65.  * pages, to let the hardware bus error or not as it sees fit.
  66.  * 
  67.  * One RAM page table is allocated per megabyte of real RAM; each table has
  68.  * 128 entries, which is 8K per page.  For a TT with 4MB ST RAM and 4MB TT RAM
  69.  * that's 8K in page tables.  You can cut this down by not allocating page
  70.  * tables for which the entire megabyte is not accessible (i.e. it's all
  71.  * private memory and it's not YOUR private memory).
  72.  * 
  73.  * You have one of these per process.  When somebody loads into G or S memory
  74.  * or leaves it, you have to go through the page tables of every process
  75.  * updating S bits (for S) and DT (for G) bits.
  76.  * 
  77.  * The top levels are small & easy so replicating them once per process
  78.  * doesn't really hurt us.
  79.  * 
  80.  */
  81.  
  82. #include "mint.h"
  83.  
  84. #if 0
  85. #define MP_DEBUG(x) DEBUG(x)
  86. #else
  87. #define MP_DEBUG(x)
  88. #endif
  89.  
  90. void *memset P_((void *s, int ucharfill, unsigned long size));
  91.  
  92. extern int debug_level;        /* see debug.c */
  93.  
  94. /*
  95.  * You can turn this whole module off, and the stuff in context.s,
  96.  * by setting no_mem_prot to 1.
  97.  */
  98.  
  99. int no_mem_prot;
  100. long page_table_size;
  101.  
  102. /*
  103.  * PMMU stuff
  104.  */
  105. #if 0
  106. #define flush_pmmu(start, end) __asm("pflusha")
  107. #else
  108. /* in cpu.spp is a new "cpush" function that can selectively flush
  109.  * the cache
  110.  */
  111. #define flush_pmmu(start, len) cpush((void *)(start), (long)(len))
  112. #endif
  113.  
  114. /*
  115.  * This is one global TC register that is copied into every process'
  116.  * context, even though it never changes.  It's also used by the
  117.  * functions that dump page tables.
  118.  */
  119.  
  120. tc_reg tc;
  121.  
  122. /* mint_top_* get used in mem.c also */
  123. ulong mint_top_tt;
  124. ulong mint_top_st;
  125.  
  126. int tt_mbytes;        /* number of megabytds of TT RAM */
  127.  
  128. /*
  129.  * global_mode_table: one byte per page in the system.  Initially all pages
  130.  * are set to "global" but then the TPA pages are set to "invalid" in
  131.  * init_mem.  This has to be allocated and initialized in init_tables,
  132.  * when you know how much memory there is.  You need a byte per page,
  133.  * from zero to the end of TT RAM, including the space between STRAM
  134.  * and TTRAM.  That is, you need 16MB/pagesize plus (tt_mbytes/pagesize)
  135.  * bytes here.
  136.  */
  137.  
  138. unsigned char *global_mode_table;
  139.  
  140. /*
  141.  * prototype descriptors; field u1 must be all ones, other u? are zero.
  142.  * This is just the first long of a full descriptor; the ".page_type" part
  143.  * of the union.  These are initialized by init_tables.
  144.  *
  145.  * The proto_page_type table yields the value to stuff into the page_type
  146.  * field of a new process' page table.  It is the "non-owner" mode for
  147.  * a page with the corresponding value in global_mode_table.
  148.  */
  149.  
  150. page_type g_page;
  151. page_type g_ci_page;
  152. page_type s_page;
  153. page_type readable_page;
  154. page_type invalid_page;
  155. page_type page_ptr;
  156.  
  157. page_type *proto_page_type[] =
  158.     { &invalid_page, &g_page, &s_page, &readable_page, &invalid_page };
  159. /*    private         global    super    private/read    invalid */
  160.  
  161. /*
  162.  * Init_tables: called sometime in initialization.  We set up some
  163.  * constants here, but that's all.  The first new_proc call will set up the
  164.  * page table for the root process and switch it in; from then on, we're
  165.  * always under some process' control.
  166.  * 
  167.  * The master page-mode table is initialized here, and some constants like
  168.  * the size needed for future page tables.
  169.  *
  170.  * One important constant initialized here is page_table_size, which is
  171.  * the amount of memory required per page table.  new_proc allocates
  172.  * this much memory for each process' page table.  This number will be
  173.  * 1K/megabyte plus page table overhead.  There are TBL_PAGES_OFFS
  174.  * tables at TBL_SIZE_BYTES each before the main tables begin; then
  175.  * there is 1024 bytes per megabyte of memory being mapped.
  176.  */
  177.  
  178. void
  179. init_tables()
  180. {
  181.     int n_megabytes;
  182.     long global_mode_table_size;
  183.  
  184.     if (no_mem_prot) return;
  185.  
  186.     TRACE(("init_tables"));
  187.  
  188. #define phys_top_tt (*(ulong *)0x5a4L)
  189.     if (phys_top_tt == 0x01000000L) mint_top_tt = 0;
  190.     else mint_top_tt = phys_top_tt;
  191.  
  192. #define phys_top_st (*(ulong *)0x42eL)
  193.     mint_top_st = phys_top_st;
  194.  
  195.     if (mint_top_tt) tt_mbytes = (mint_top_tt - 0x01000000) / ONE_MEG;
  196.     else tt_mbytes = 0;
  197.  
  198.     n_megabytes = (mint_top_st / ONE_MEG) + tt_mbytes;
  199.  
  200.     /*
  201.      * page table size: room for A table, B0 table, BF table, STRAM C
  202.      * table, one TTRAM C table per 16MB (or fraction) of TTRAM, and 1024
  203.      * bytes per megabyte.
  204.      */
  205.  
  206.     page_table_size = (4L * TBL_SIZE_BYTES) +
  207.               (((tt_mbytes+15L)/16L) * TBL_SIZE_BYTES) +
  208.               (n_megabytes*1024L);
  209.  
  210.     global_mode_table_size = ((SIXTEEN_MEG / QUANTUM) +
  211.                 (((ulong)tt_mbytes * ONE_MEG) / QUANTUM));
  212.  
  213.     global_mode_table = kmalloc(global_mode_table_size);
  214.  
  215.     assert(global_mode_table);
  216.  
  217.     TRACELOW(("mint_top_st is $%lx; mint_top_tt is $%lx, n_megabytes is %d",
  218.     mint_top_st, mint_top_tt, n_megabytes));
  219.     TRACELOW(("page_table_size is %ld, global_mode_table_size %ld",
  220.         page_table_size,
  221.         global_mode_table_size));
  222.  
  223.     g_page.limit = 0x7fff;    /* set nonzero fields: disabled limit */
  224.     g_page.unused1 = 0x3f;    /* ones in this reserved field */
  225.     g_page.unused2 = 0;
  226.     g_page.s = 0;
  227.     g_page.unused3 = 0;
  228.     g_page.ci = 0;
  229.     g_page.unused4 = 0;
  230.     g_page.m = 1;        /* set m and u to 1 so CPU won't do writes */
  231.     g_page.u = 1;
  232.     g_page.wp = 0;        /* not write-protected */
  233.     g_page.dt = 1;        /* descriptor type 1: page descriptor */
  234.  
  235.     g_ci_page = g_page;
  236.     g_ci_page.ci = 1;
  237.  
  238.     readable_page = g_page;    /* a page which is globally readable */
  239.     readable_page.wp = 1;    /* but write protected */
  240.  
  241.     s_page = g_page;        /* a page which is globally accessible */
  242.     s_page.s = 1;        /* if you're supervisor */
  243.  
  244.     invalid_page = g_page;
  245.     invalid_page.dt = 0;
  246.  
  247.     page_ptr = g_page;
  248.     page_ptr.m = 0;        /* this must be zero in page pointers */
  249.     page_ptr.dt = 3;
  250.  
  251.     tc.enable = 1;
  252.     tc.zeros = 0;
  253.     tc.sre = 0;
  254.     tc.fcl = 0;
  255.     tc.is = 0;
  256.     tc.tia = 4;
  257.     tc.tib = 4;
  258.     tc.tic = 4;
  259.     tc.tid = 7;            /* 0+4+4+4+7+13 == 32 */
  260.     tc.ps = 13;            /* 8K page size */
  261.  
  262.     /* set the whole global_mode_table to "global" */
  263.     memset(global_mode_table,PROT_G,global_mode_table_size);
  264. }
  265.  
  266. /*
  267.  * mark_region: mark a region of memory as having a particular type.
  268.  * The arguments are the memory region in question and the new type.
  269.  * If the new type is zero then the old type is preserved.  The
  270.  * type of each page is kept in a global place for this purpose,
  271.  * among others.
  272.  *
  273.  * The types are:
  274.  *  0    private
  275.  *  1    global
  276.  *  2    private, but super-accessible
  277.  *  3    private, but world readable
  278.  *  4   invalid
  279.  *
  280.  
  281. The idea is this:
  282.  
  283.     for (each process) {
  284.     if (you're an owner or you're special) {
  285.         set up owner modes
  286.     }
  287.     else {
  288.         set up non-owner modes
  289.     }
  290.  
  291.     mark_pages(pagetbl,start,len,modes);
  292.     }
  293.  
  294.  */
  295.  
  296. /*
  297.             invalid---v
  298.           private/gr---v  |
  299.             super-------v  |  |
  300.         global-------v  |  |  |
  301.     private-------v     |  |  |  |
  302.               |     |  |  |  |
  303. */
  304. ushort other_dt[] = { 0, 1, 1, 1, 0 };
  305. ushort other_s[] = {  0, 0, 1, 0, 0 };
  306. ushort other_wp[] = { 0, 0, 0, 1, 0 };
  307.  
  308.  
  309. /*
  310.  * get_page_cookie: return a cookie representing the protection status
  311.  * of some memory.
  312.  *
  313.  * Returns ((wp << 3) | (s << 2) | (dt) | 0x8000) when it wins.
  314.  * Returns 1 if the pages are not all controlled, 0 if they're not all the same.
  315.  */
  316.  
  317. static short
  318. get_page_cookie(long_desc *base_tbl,ulong start,ulong len)
  319. {
  320.     int b_index, c_index, d_index;
  321.     long_desc *tbl;
  322.     int dt, s, wp;
  323.  
  324.     if (start < mint_top_st) {
  325.     /* start is in ST RAM; fail if not entirely in ST RAM */
  326.     if (start+len > mint_top_st) {
  327.         return 1;
  328.     }
  329.     }
  330.     else if (start >= 0x01000000 && start < mint_top_tt) {
  331.     /* start is in TT RAM; fail if not entirely in TT RAM */
  332.     if (start+len > mint_top_tt) {
  333.         return 1;
  334.     }
  335.     }
  336.  
  337.     b_index = (start >> LOG2_16_MEG);
  338.     c_index = (start >> LOG2_ONE_MEG) & 0xf;
  339.     d_index = (start >> LOG2_EIGHT_K) & 0x7f;
  340.  
  341.     tbl = &(base_tbl[0].
  342.         tbl_address[b_index].
  343.         tbl_address[c_index].
  344.             tbl_address[d_index]);
  345.  
  346.     dt = tbl->page_type.dt;
  347.     wp = tbl->page_type.wp;
  348.     s = tbl->page_type.s;
  349.  
  350.     /*
  351.      * This can be optimized: while in a single megabyte, a quick loop on
  352.      * successive long_desc's will work; similarly, while in a single 16MB,
  353.      * an outer loop on the same C-level table works.
  354.      */
  355.  
  356.     while (len) {
  357.     /*
  358.      * a_index is always zero.  Only the first 256MB is mapped.
  359.      * b_index is the 16MB number of the page.
  360.      * c_index is the 1MB number of that page within the 16MB (0-15)
  361.      * d_index is the 8K number within that 1MB (0-127).
  362.      */
  363.     b_index = (start >> LOG2_16_MEG);
  364.     c_index = (start >> LOG2_ONE_MEG) & 0xf;
  365.     d_index = (start >> LOG2_EIGHT_K) & 0x7f;
  366.  
  367.     tbl = base_tbl;            /* tbl := start of A table  */
  368.     tbl = tbl[0].tbl_address;    /*           B table  */
  369.     tbl = tbl[b_index].tbl_address;    /*           C table  */
  370.     tbl = tbl[c_index].tbl_address;    /*           D table  */
  371.     tbl = &tbl[d_index];        /* tbl := addr of page entry */
  372.  
  373.     if ((tbl->page_type.dt != dt) ||
  374.         (tbl->page_type.s != s) ||
  375.         (tbl->page_type.wp != wp)) {
  376.             /* fail because it's not all the same protection */
  377.         return 0;
  378.     }
  379.     len -= EIGHT_K;
  380.     start += EIGHT_K;
  381.     }
  382.     /* we passed -- all the pages in question have the same prot. status */
  383.     return (wp << 3) | (s << 2) | dt | 0x8000;
  384. }
  385.  
  386. static void
  387. mark_pages(long_desc *base_tbl,ulong start,ulong len,
  388.         ushort dt_val, ushort s_val, ushort wp_val, PROC *proc)
  389. {
  390.     int b_index, c_index, d_index;
  391.     long_desc *tbl;
  392.     ulong oldstart, oldlen;
  393.  
  394.     if (no_mem_prot) return;
  395.  
  396.     oldstart = start;
  397.     oldlen = len;
  398.  
  399. #ifdef MEMPROT_SHORTCUT
  400.     /*
  401.      * Take a shortcut here: we're done if first page of the region is
  402.      * already right. We duplicate the top of the "while" loop to do it,
  403.      * but what the heck.
  404.      */
  405.     /* I don't think this shortcut is a good idea, since while we
  406.      * are doing Mshrink or Srealloc we may very well have a region
  407.      * with mixed page types -- ERS
  408.      */
  409.  
  410.     b_index = (start >> LOG2_16_MEG);
  411.     c_index = (start >> LOG2_ONE_MEG) & 0xf;
  412.     d_index = (start >> LOG2_EIGHT_K) & 0x7f;
  413.  
  414.     tbl = &(base_tbl[0].
  415.         tbl_address[b_index].
  416.         tbl_address[c_index].
  417.             tbl_address[d_index]);
  418.  
  419.     if (tbl->page_type.dt == dt_val &&
  420.     tbl->page_type.s == s_val &&
  421.     tbl->page_type.wp == wp_val) {
  422. /*
  423.         TRACE(("mark_pages a:0 b:%d c:%d d:%d (same)",
  424.             b_index,c_index,d_index));
  425. */
  426.         return;
  427.     }
  428.  
  429. #endif /* MEMPROT_SHORTCUT */
  430. /*
  431.     MP_DEBUG(("mark_pages a:0 b:%d c:%d d:%d (diff)",b_index,c_index,d_index));
  432. */
  433.  
  434.     /*
  435.      * This can be optimized: while in a single megabyte, a quick loop on
  436.      * successive long_desc's will work; similarly, while in a single 16MB,
  437.      * an outer loop on the same C-level table works.
  438.      */
  439.  
  440.     while (len) {
  441.     /*
  442.      * a_index is always zero.  Only the first 256MB is mapped.
  443.      * b_index is the 16MB number of the page.
  444.      * c_index is the 1MB number of that page within the 16MB (0-15)
  445.      * d_index is the 8K number within that 1MB (0-127).
  446.      */
  447.     b_index = (start >> LOG2_16_MEG);
  448.     c_index = (start >> LOG2_ONE_MEG) & 0xf;
  449.     d_index = (start >> LOG2_EIGHT_K) & 0x7f;
  450.  
  451.     tbl = base_tbl;            /* tbl := start of A table  */
  452.     tbl = tbl[0].tbl_address;    /*           B table  */
  453.     tbl = tbl[b_index].tbl_address;    /*           C table  */
  454.     tbl = tbl[c_index].tbl_address;    /*           D table  */
  455.     tbl = &tbl[d_index];        /* tbl := addr of page entry */
  456.  
  457.     tbl->page_type.dt = dt_val;
  458.     tbl->page_type.s = s_val;
  459.     tbl->page_type.wp = wp_val;
  460.     len -= EIGHT_K;
  461.     start += EIGHT_K;
  462.     }
  463.  
  464. #if 0
  465.     /*
  466.      * Flush the mmu address translation cache only if we changed curproc's
  467.      * table.
  468.      */
  469.  
  470.     if (proc == curproc) flush_pmmu(oldstart, oldlen);
  471. #else
  472.     flush_pmmu(oldstart, oldlen);
  473. #endif
  474. }
  475.  
  476. /* get_prot_mode(r): returns the type of protection region r
  477.  * has
  478.  */
  479.  
  480. int
  481. get_prot_mode(r)
  482.     MEMREGION *r;
  483. {
  484.     ulong start = r->loc;
  485.  
  486.     if (no_mem_prot)
  487.         return PROT_G;
  488.     return global_mode_table[(start >> 13)];
  489. }
  490.  
  491. void
  492. mark_region(region,mode)
  493. MEMREGION *region;
  494. short mode;
  495. {
  496.     ulong start = region->loc;
  497.     ulong len = region->len;
  498.     ulong i;
  499.     ushort dt_val, s_val, wp_val;
  500.     PROC *proc;
  501.     MEMREGION **mr;
  502.     int change;
  503.  
  504.     if (no_mem_prot) return;
  505.  
  506.     MP_DEBUG(("mark_region %lx len %lx mode %d",start,len,mode));
  507.     
  508.     if (mode == PROT_NOCHANGE) {
  509.     mode = global_mode_table[(start >> 13)];
  510.     }
  511.  
  512.     if (global_mode_table[(start >> 13)] == mode) {
  513.         change = 0;
  514.     }
  515.     else change = 1;
  516.  
  517.     /* mark the global page table */
  518.  
  519.     memset(&global_mode_table[start >> 13],mode,(len >> 13));
  520.  
  521.     for (proc = proclist; proc; proc = proc->gl_next) {
  522.     assert(proc->page_table);
  523.     if (mode == PROT_I || mode == PROT_G) {
  524.         /* everybody gets the same flags */
  525.         goto notowner;
  526.     }
  527.     if (proc->memflags & F_OS_SPECIAL) {
  528.         /* you're special; you get owner flags */
  529.         MP_DEBUG(("mark_region: pid %d is an OS special!",proc->pid));
  530.         goto owner;
  531.     }
  532.     if (mr = proc->mem) {
  533.         for (i = 0; i < proc->num_reg; i++, mr++) {
  534.         if (*mr == region) {
  535.             MP_DEBUG(("mark_region: pid %d is an owner",proc->pid));
  536. owner:
  537.             dt_val = 1;
  538.             s_val = 0;
  539.             wp_val = 0;
  540.             goto gotvals;
  541.         }
  542.         }
  543.     }
  544.  
  545. notowner:
  546.  
  547. /* if you get here you're not an owner, or mode is G or I */
  548.     MP_DEBUG(("mark_region: pid %d gets non-owner modes",proc->pid));
  549.  
  550.     dt_val = other_dt[mode];
  551.     s_val = other_s[mode];
  552.     wp_val = other_wp[mode];
  553.  
  554. gotvals:
  555.     mark_pages(proc->page_table,start,len,dt_val,s_val,wp_val,proc);
  556.     }
  557. }
  558.  
  559. /*
  560.  * prot_temp: temporarily alter curproc's access to memory.
  561.  * Pass in a -1 to give curproc global access; returns a cookie.  Call
  562.  * again with that cookie to return the memory to the old mode.
  563.  * There should be no context switches or memory protection changes
  564.  * in the meantime.
  565.  *
  566.  * If called with mode == -1, returns...
  567.  *    -1 if mem prot is off -- no error, no action.
  568.  *     0 if the pages are not all the same.
  569.  *     1 if the pages are not all controlled by the page tables.
  570.  *
  571.  * When mode != -1, returns...
  572.  *    0 for success (should never fail).  There is little checking.
  573.  * Calling with mode == 0 or 1 results in zero to spoof success, but in fact
  574.  * this is an error.  Mode is only really valid if (mode & 0x8000).
  575.  */
  576.  
  577. int
  578. prot_temp(loc,len,mode)
  579. ulong loc;
  580. ulong len;
  581. int mode;
  582. {
  583.     int cookie;
  584.  
  585.     if (no_mem_prot) return -1;
  586.  
  587.     /* round start down to the previous page and len up to the next one. */
  588.     loc &= ~MASKBITS;
  589.     len = ROUND(len);
  590.  
  591.     if (mode == 0 || mode == 1) return 0;    /* do nothing */
  592.     if (mode == -1) {
  593.     cookie = get_page_cookie(curproc->page_table,loc,len);
  594.  
  595.     /* if not all controlled, return status */
  596.     if (cookie == 0 || cookie == 1) return cookie;
  597.  
  598.     mark_pages(curproc->page_table,loc,len,1,0,0,curproc);
  599.  
  600.     return cookie;
  601.     }
  602.     else {
  603.     mark_pages(curproc->page_table,loc,len,
  604.             mode&3,mode&4,mode&8,curproc);
  605.     return 0;
  606.     }
  607. }
  608.  
  609. /*
  610.  * init_page_table: fill in the page table for the indicated process. The
  611.  * master page map is consulted for the modes of all pages, and the memory
  612.  * region data structures are consulted to see if this process is the owner
  613.  * of any of those tables.
  614.  *
  615.  * This also sets crp and tc in both ctxts of the process.  If this is the
  616.  * first call, then the CPU tc is cleared, the TT0 and TT1 regs are zapped,
  617.  * and then this proc's crp and tc are loaded into it.
  618.  */
  619.  
  620. static short mmu_is_set_up = 0;
  621.  
  622. void
  623. init_page_table(proc)
  624. PROC *proc;
  625. {
  626.     long_desc *tptr;
  627.     long_desc *tbl_a;        /* top-level table */
  628.     long_desc *tbl_b0;        /* second level, handles $0 nybble */
  629.     long_desc *tbl_bf;        /* handles $F nybble */
  630.     long_desc *tbl_c;        /* temp pointer to start of 16MB */
  631.     ulong p, q, r;
  632.     ulong i, j, k;
  633.     int g;
  634.     MEMREGION **mr;
  635.  
  636.     if (no_mem_prot) return;
  637.  
  638.     if (proc->pid)
  639.         TRACELOW(("init_page_table(proc=%lx, pid %d)",proc,proc->pid));
  640.  
  641.     assert(proc && proc->page_table);
  642.  
  643.     tptr = proc->page_table;
  644.     tbl_a = tptr;
  645.     tptr += TBL_SIZE;
  646.     tbl_b0 = tptr;
  647.     tptr += TBL_SIZE;
  648.     tbl_bf = tptr;
  649.     tptr += TBL_SIZE;
  650.  
  651.     /*
  652.      * table A indexes by the first nybble: $0 and $F refer to their tables,
  653.      * $1-$7 are uncontrolled, cacheable; $8-$E are uncontrolled, ci.
  654.      */
  655.  
  656.     tbl_a[0].page_type = page_ptr;
  657.     tbl_a[0].tbl_address = tbl_b0;
  658.  
  659.     for (i=1; i<0xf; i++) {
  660.     if (i < 8)  tbl_a[i].page_type = g_page;
  661.     else        tbl_a[i].page_type = g_ci_page;
  662.     tbl_a[i].tbl_address = (long_desc *)(i << 28);
  663.     }
  664.  
  665.     /* $F entry of table A refers to table BF */
  666.     tbl_a[0xf].page_type = page_ptr;
  667.     tbl_a[0xf].tbl_address = tbl_bf;
  668.  
  669.     /*
  670.      * table B0: entry 0 is $00, the 16MB of ST address space.
  671.      */
  672.  
  673.     tbl_b0[0].page_type = page_ptr;
  674.     tbl_b0[0].tbl_address = tptr;
  675.     tbl_c = tptr;
  676.     tptr += TBL_SIZE;
  677.  
  678.     /* for each megabyte that is RAM, allocate a table */
  679.     for (i = 0, k = 0, p = 0; p < mint_top_st; i++, p += 0x00100000) {
  680.     tbl_c[i].page_type = page_ptr;
  681.     tbl_c[i].tbl_address = tptr;
  682.  
  683.     /* for each page in this megabyte, write a page entry */
  684.     for (q = p, j = 0; j < 128; j++, q += 0x2000, k++) {
  685.         tptr->page_type = *proto_page_type[global_mode_table[k]];
  686.         tptr->tbl_address = (long_desc *)q;
  687.         tptr++;
  688.     }
  689.     }
  690.  
  691.     /* now for each megabyte from mint_top_st to ROM, mark global */
  692.     for ( ; p < 0x00E00000; i++, p += 0x00100000) {
  693.     tbl_c[i].page_type = g_page;
  694.     tbl_c[i].tbl_address = (long_desc *)p;
  695.     }
  696.  
  697.     /* fill in the E and F tables: 00Ex is ROM, 00Fx is I/O  */
  698.     tbl_c[i].page_type = g_page;
  699.     tbl_c[i].tbl_address = (long_desc *)p;
  700.     i++, p += 0x00100000;
  701.     tbl_c[i].page_type = g_ci_page;
  702.     tbl_c[i].tbl_address = (long_desc *)p;
  703.  
  704.         /* Done with tbl_c for 0th 16MB; go on to TT RAM */
  705.  
  706. /* 
  707.     structure:
  708.  
  709.     for (i = each 16MB that has any TT RAM in it)
  710.     allocate a table tbl_c, point tbl_b0[i] at it
  711.     for (j = each 1MB that is RAM)
  712.         allocate a table, point tbl_c[j] at it
  713.         for (k = each page in the megabyte)
  714.         fill in tbl_c[j][k] with page entry from global_mode_table
  715.     for (j = the rest of the 16MB)
  716.         set tbl_c[j] to "global, cacheable"
  717.  
  718.     for (i = the rest of the 16MBs from here to $7F)
  719.     set tbl_b0[i] to "global, cacheable"
  720.  
  721.     for (i = the rest of the 16MBs from $80 up to but not incl. $FF)
  722.     set tbl_b0[i] to "global, not cacheable"
  723. */
  724.  
  725.     /* i counts 16MBs */
  726.     for (i = 1, p = 0x01000000, g = 2048;
  727.      p < mint_top_tt;
  728.      p += SIXTEEN_MEG, i++) {
  729.         tbl_b0[i].page_type = page_ptr;
  730.         tbl_b0[i].tbl_address = tptr;
  731.         tbl_c = tptr;
  732.         tptr += TBL_SIZE;
  733.  
  734.         /* j counts MBs */
  735.         for (j = 0, q = p; j < 16 && q < mint_top_tt; q += ONE_MEG, j++) {
  736.         tbl_c[j].page_type = page_ptr;
  737.         tbl_c[j].tbl_address = tptr;
  738.         /* k counts pages (8K) */
  739.         for (r = q, k = 0; k < 128; k++, r += 0x2000, g++) {
  740.             tptr->page_type = *proto_page_type[global_mode_table[g]];
  741.             tptr->tbl_address = (long_desc *)r;
  742.             tptr++;
  743.         }
  744.         }
  745.         for ( ; j < 16; j++, q += ONE_MEG) {
  746.         /* fill in the rest of this 16MB */
  747.         tbl_c[j].page_type = g_page;
  748.         tbl_c[j].tbl_address = (long_desc *)q;
  749.         }
  750.     }
  751.  
  752.     /* fill in the rest of $00-$0F as cacheable */
  753.     for ( ; i < 16; i++, p += SIXTEEN_MEG) {
  754.     tbl_b0[i].page_type = g_page;
  755.     tbl_b0[i].tbl_address = (long_desc *)p;
  756.     }
  757.  
  758.     /* done with TT RAM in table b0; do table bf */
  759.  
  760.     /*
  761.      * Table BF: translates addresses starting with $F.  First 15 are
  762.      * uncontrolled, cacheable; last one translates $FF, which
  763.      * which shadows $00 (the 16MB ST address space).  The rest
  764.      * are uncontrolled, not cacheable.
  765.      *
  766.      * The table address of the copy has a 1 in the low (unused) bit, which
  767.      * is a signal to the table dumper not to dump this, as it's a copy
  768.      * of tbl_b0[0].
  769.      */
  770.  
  771.     for (i=0; i<0xf; i++) {
  772.     tbl_bf[i].page_type = g_ci_page;
  773.     tbl_bf[i].tbl_address = (long_desc *)((i << 24) | 0xf0000000);
  774.     }
  775.     tbl_bf[0xf] = tbl_b0[0];
  776.     *(ulong *)(&(tbl_bf[0xf].tbl_address)) |= 1;
  777.  
  778.     proc->ctxt[0].crp.limit = 0x7fff;    /* disable limit function */
  779.     proc->ctxt[0].crp.dt = 3;        /* points to valid 8-byte entries */
  780.     proc->ctxt[0].crp.tbl_address = tbl_a;
  781.     proc->ctxt[1].crp = proc->ctxt[0].crp;
  782.     proc->ctxt[0].tc = tc;
  783.     proc->ctxt[1].tc = tc;
  784.  
  785.     /*
  786.      * OK, memory tables are now there as if you're a non-owner of every
  787.      * page.  Now for each region you ARE an owner of, mark with owner
  788.      * modes.
  789.      */
  790.  
  791.     mr = proc->mem;
  792.     for (i=0; i < proc->num_reg; i++, mr++) {
  793.     if (*mr) {
  794.             mark_pages(proc->page_table,(*mr)->loc,(*mr)->len,1,0,0,proc);
  795.         }
  796.     }
  797.  
  798.     if (!mmu_is_set_up) {
  799.     set_mmu(proc->ctxt[0].crp,proc->ctxt[0].tc);
  800.     mmu_is_set_up = 1;
  801.     }
  802. }
  803.  
  804. /*
  805.  * This routine is called when procfs detects that a process wants to be an
  806.  * OS SPECIAL.  The AES, SCRENMGR, and DESKTOP do this, and so does FSMGDOS
  807.  * and possibly some other stuff. It has to re-mark every page in that
  808.  * process' page table based on its new special status. The "special
  809.  * status" is "you get global access to all of memory" and "everybody
  810.  * gets Super access to yours."  It is the caller's responsibility
  811.  * to set proc's memflags, usually to (F_OS_SPECIAL | F_PROT_S).
  812.  */
  813.  
  814. void
  815. mem_prot_special(proc)
  816. PROC *proc;
  817. {
  818.     MEMREGION **mr;
  819.     int i;
  820.  
  821.     if (no_mem_prot) return;
  822.  
  823.     TRACE(("mem_prot_special(pid %d)",proc->pid));
  824.  
  825.     /*
  826.      * This marks ALL memory, allocated or not, as accessible. When memory
  827.      * is freed even F_OS_SPECIAL processes lose access to it. So one or
  828.      * the other of these is a bug, depending on how you want it to work.
  829.      */
  830.     mark_pages(proc->page_table,0,mint_top_st,1,0,0,proc);
  831.     if (mint_top_tt) {
  832.     mark_pages(proc->page_table,
  833.             0x01000000,
  834.             mint_top_tt - 0x01000000,
  835.             1,0,0,
  836.             proc);
  837.     }
  838.  
  839.     /*
  840.      * In addition, mark all the pages the process already owns as "super"
  841.      * in all other processes.  Thus the "special" process can access all
  842.      * of memory, and any process can access the "special" process' memory
  843.      * when in super mode.
  844.      */
  845.  
  846.     mr = proc->mem;
  847.  
  848.     for (i=0; i < proc->num_reg; i++, mr++) {
  849.     if (*mr) {
  850.         mark_region(*mr,PROT_S);
  851.     }
  852.     }
  853. }
  854.     
  855. /*----------------------------------------------------------------------------
  856.  * DEBUGGING SECTION
  857.  *--------------------------------------------------------------------------*/
  858.  
  859. static void
  860. _dump_tree(tbl,level)
  861. long_desc tbl;
  862. int level;
  863. {
  864.     int i, j;
  865.     long_desc *p;
  866.     static char spaces[9] = "        ";
  867.  
  868.     /* print the level and display the table descriptor */
  869.     FORCE("\r%s s:%x wp:%x dt:%x a:%08lx",
  870.     &spaces[8-(level*2)],
  871.     tbl.page_type.s,
  872.     tbl.page_type.wp,
  873.     tbl.page_type.dt,
  874.     tbl.tbl_address);
  875.  
  876.     if (tbl.page_type.dt == 3) {
  877.     if (level == 0) {
  878.         j = (1 << tc.tia);
  879.     }
  880.     else if (level == 1) {
  881.         j = (1 << tc.tib);
  882.     }
  883.     else if (level == 2) {
  884.         j = (1 << tc.tic);
  885.     }
  886.     else {
  887.         j = (1 << tc.tid);
  888.     }
  889.  
  890.     /* don't show table if it's the duplicate */
  891.     if ((ulong)tbl.tbl_address & 1) return;
  892.  
  893.     ++level;
  894.     p = tbl.tbl_address;
  895.     for (i=0; i<j; i++, p++) {
  896.         _dump_tree(*p,level);
  897.     }
  898.     }
  899. }
  900.  
  901. static char modesym[] = { 'p', 'g', 's', 'r', 'i' };
  902.  
  903. void
  904. QUICKDUMP()
  905. {
  906.     char outstr[33];
  907.     ulong i, j, end;
  908.  
  909.     if (no_mem_prot) return;
  910.  
  911.     FORCE("STRAM global table:");
  912.     outstr[32] = '\0';
  913.     end = mint_top_st / QUANTUM;
  914.     for (i = 0; i < end; i += 32) {
  915.     for (j=0; j<32; j++) {
  916.         outstr[j] = modesym[global_mode_table[j+i]];
  917.     }
  918.     FORCE("%08lx: %s",i*8192L,outstr);
  919.     }
  920.  
  921.     if (mint_top_tt) {
  922.     FORCE("TTRAM global table:");
  923.     end = mint_top_tt / QUANTUM;
  924.     for (i = 2048; i < end; i += 32) {
  925.         for (j=0; j<32; j++) {
  926.         outstr[j] = modesym[global_mode_table[j+i]];
  927.         }
  928.         FORCE("%08lx: %s",i*8192L,outstr);
  929.     }
  930.     }
  931. }
  932.  
  933. char *berr_msg[] = { 
  934. /*  "........." */
  935.     "private  ",
  936.     "global   ",    /* turned into "hardware" for violation reports */
  937.     "super    ",
  938.     "readable ",
  939.     "free     ",
  940.     "hardware "        /* used when the memory is not controlled by us */
  941. };
  942.  
  943. void
  944. report_buserr()
  945. {
  946.     long_desc *tbl;
  947.     char *vmsg = NULL;
  948.     short mode;
  949.     ulong aa, pc;
  950.     char alertbuf[5*32+16];    /* enough for an alert */
  951.     char *aptr;
  952.  
  953.     if (no_mem_prot) return;
  954.  
  955.     tbl = (long_desc *)curproc->exception_tbl;
  956.     aa = curproc->exception_addr;
  957.     pc = curproc->exception_pc;
  958.     if ((mint_top_tt && aa < mint_top_tt) || (aa < mint_top_st)) {
  959.     mode = global_mode_table[(curproc->exception_addr >> 13)];
  960.     if (mode == PROT_G) {
  961.         /* page is global: obviously a hardware bus error */
  962.         mode = 5;
  963.     }
  964.     }
  965.     else {
  966.     /* (addr is > mint_top_tt) set mode = 5 so we don't look for owners */
  967.         mode = 5;
  968.     }
  969.     vmsg = berr_msg[mode];
  970.  
  971.     /* construct an AES alert box for this error:
  972.     | PROCESS  "buserrxx"  KILLED: |
  973.     | MEMORY VIOLATION.  (PID 000) |
  974.     |                              |
  975.     | Type: ......... PC: pc...... |
  976.     | Addr: ........  BP: ........ |
  977.     */
  978.  
  979.     /* we play games to get around 128-char max for ksprintf */
  980.     ksprintf(alertbuf,"[1][ PROCESS  \"%s\"  KILLED: |",curproc->name);
  981.     aptr = alertbuf + strlen(alertbuf);
  982.     ksprintf(aptr," MEMORY VIOLATION.  (PID %03d) | |",curproc->pid);
  983.     aptr = alertbuf + strlen(alertbuf);
  984.     ksprintf(aptr," Type: %s PC: %08lx |",vmsg,pc);
  985.     aptr = alertbuf + strlen(alertbuf);
  986.     ksprintf(aptr," Addr: %08lx  BP: %08lx ][ OK ]",aa,curproc->base);
  987.     if (!_ALERT(alertbuf)) {
  988.         /* this will call _alert again, but it will just fail again */
  989.         ALERT("MEMORY VIOLATION: type=%s AA=%lx PC=%lx BP=%lx",
  990.             vmsg,aa,pc,curproc->base);
  991.     }
  992.         
  993.     if (curproc->pid == 0 || curproc->memflags & F_OS_SPECIAL) {
  994.     /* the system is so thoroughly hosed that anything we try will
  995.      * likely cause another bus error; so let's just hang up
  996.      */
  997.     FATAL("Operating system killed");
  998.     }
  999. }
  1000.  
  1001. /*
  1002.  * big_mem_dump is a biggie: for each page in the system, it
  1003.  * displays the PID of the (first) owner and the protection mode.
  1004.  * The output has three chars per page, and eight chars per line.
  1005.  * The first page of a region is marked with the mode, and the
  1006.  * rest with a space.
  1007.  *
  1008.  * Logic:
  1009.     for (mp = *core; mp; mp++) {
  1010.     for (each page of this region) {
  1011.         if (start of line) {
  1012.         output line starter;
  1013.         }
  1014.         if (start of region) {
  1015.         output mode of this page;
  1016.         determine owner;
  1017.         output owner;
  1018.         }
  1019.         else {
  1020.         output space;
  1021.         output owner;
  1022.         }
  1023.         }
  1024.     }
  1025.  */
  1026.  
  1027. void
  1028. BIG_MEM_DUMP(bigone,proc)
  1029. int bigone;
  1030. PROC *proc;
  1031. {
  1032. #ifndef NO_DEBUG_INFO
  1033.     char linebuf[128];
  1034.     char *lp = linebuf;
  1035.     MEMREGION *mp, **mr, **map;
  1036.     PROC *p;
  1037.     ulong loc;
  1038.     short owner = 0;
  1039.     short i;
  1040.     short first;
  1041.  
  1042.  
  1043.   if (no_mem_prot) return;
  1044.  
  1045.   for (map = core; map != 0; ((map == core) ? (map = alt) : (map = 0))) {
  1046.     FORCE("Annotated memory dump for %s",(map == core ? "core" : "alt"));
  1047.     first = 1;
  1048.     *linebuf = '\0';
  1049.     for (mp = *map; mp; mp = mp->next) {
  1050.     for (loc = mp->loc; loc < (mp->loc + mp->len); loc += EIGHT_K) {
  1051.         if (first || ((loc & 0x1ffff) == 0)) {
  1052.         if (*linebuf) FORCE(linebuf);
  1053.         ksprintf(linebuf,"\r%08lx: ",loc);
  1054.         lp = &linebuf[11];
  1055.         first = 0;
  1056.         }
  1057.         if (loc == mp->loc) {
  1058.         *lp++ = modesym[global_mode_table[loc / EIGHT_K]];
  1059.  
  1060.         for (p = proclist; p; p = p->gl_next) {
  1061.             if (p->mem) {
  1062.             mr = p->mem;
  1063.             for (i=0; i < p->num_reg; i++, mr++) {
  1064.                 if (*mr == mp) {
  1065.                 owner = p->pid;
  1066.                 goto gotowner;
  1067.                 }
  1068.             }
  1069.             }
  1070.         }
  1071.         owner = 000;
  1072. gotowner:
  1073.         ksprintf(lp,"%03d",owner);
  1074.         lp += 3;
  1075.         }
  1076.         else {
  1077.         *lp++ = ' ';
  1078.         *lp++ = '-';
  1079.         *lp++ = '-';
  1080.         *lp++ = '-';
  1081.         *lp = '\0';    /* string is always null-terminated */
  1082.         }
  1083.         }
  1084.     }
  1085.     FORCE(linebuf);
  1086.   }
  1087.  
  1088.     if (bigone) {
  1089.     long_desc tbl;
  1090.  
  1091.     /* fill in tbl with the only parts used at the top level */
  1092.     tbl.page_type.dt = proc->ctxt[CURRENT].crp.dt;
  1093.     tbl.tbl_address = proc->ctxt[CURRENT].crp.tbl_address;
  1094.     _dump_tree(tbl,0);
  1095.     }
  1096. #endif /* NO_DEBUG_INFO */
  1097. }
  1098.  
  1099.  
  1100. /*
  1101.  * Can the process "p" access the "nbytes" long
  1102.  * block of memory starting at "start"?
  1103.  * If it would be a legal access, the current
  1104.  * process is given temporary access via
  1105.  * prot_temp.
  1106.  * Returns a cookie like the one prot_temp
  1107.  * returns; if the process shouldn't have
  1108.  * access to the memory, returns 1.
  1109.  *
  1110.  * BUG: should actually read p's page table to
  1111.  * determine access
  1112.  */
  1113.  
  1114. int
  1115. mem_access_for(p, start, nbytes)
  1116.     PROC *p;
  1117.     ulong start;
  1118.      long nbytes;
  1119. {
  1120.     MEMREGION **mr;
  1121.     int i;
  1122.  
  1123.     if (no_mem_prot) return -1;
  1124.     if (start >= (ulong)p && start+nbytes <= (ulong)(p+1))
  1125.         return -1;
  1126.     if (p == rootproc)
  1127.         goto win_and_mark;
  1128.  
  1129.     mr = p->mem;
  1130.     if (mr) {
  1131.         for (i = 0; i < p->num_reg; i++, mr++) {
  1132.         if (*mr) {
  1133.             if (((*mr)->loc <= start) &&
  1134.             ((*mr)->loc + (*mr)->len >= start + nbytes))
  1135.                 goto win_and_mark;
  1136.         }
  1137.         }
  1138.     }
  1139.  
  1140.     return 0;    /* we don't own this memory */
  1141.  
  1142. win_and_mark:
  1143.     return prot_temp(start, nbytes, -1);
  1144. }
  1145.